﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OMS.OMSService;
using OMS.Models;
using PagedList;

namespace OMS.Controllers
{
    public class AdminController : Controller
    {
        //
        // GET: /Admin/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult AddUser()
        {
            return View();
        }

        [HttpPost]
        public ActionResult AddUser(UserModelView userModelView)
        {
            bool addRes = false, addResSpec = false;
            if (ModelState.IsValid)
            {
                Service1 service = new Service1();
                UserModel userModel = userModelView.ToUserModel();
                service.CreateUser(userModel, out addRes, out addResSpec);
            }
            if (addRes)
                return RedirectToAction("GetAllUsers");
            else
                return RedirectToAction("AddUser"); //TODO: RedirectToError
        }

        public ActionResult GetUser(int id)
        {
            //TODO: Test
            Service1 service = new Service1();
            UserModelView userModelView = new UserModelView(service.GetUserById(id, true));
            return View(userModelView); //TODO: View
        }

        public ActionResult GetAllUsers(int? page)
        {
            //TODO: Test
            Service1 service = new Service1();
            List<UserModel> users = service.GetAllUsers().ToList();
            ViewBag.UserCount = users.Count;
            List<UserModelView> userModelViews = new List<UserModelView>();
            foreach (var user in users)
            {
                userModelViews.Add(new UserModelView(user));
            }
            int pageSize = 5;
            int pageNumber = (page ?? 1);
            return View(userModelViews.ToPagedList(pageNumber, pageSize));
        }

        public ActionResult EditUser(int id)
        {
            Service1 service = new Service1();
            UserModelView userModelView = new UserModelView(service.GetUserById(id,true));
            return View(userModelView);
        }

        [HttpPost]
        public ActionResult EditUser(UserModelView userModelView)
        {
            bool editRes = false, editResSpec = false;
            UserModel userModel = userModelView.ToUserModel();
            if (!ModelState.IsValid)
            { 
                return RedirectToAction("EditUser");
            }
            else
            {
                Service1 service = new Service1();
                service.EditUser(userModel, out editRes, out editResSpec);
            }
            if (editRes)
                return RedirectToAction("GetAllUsers");
            else
                return RedirectToAction("EditUser");
        }

        public ActionResult Filter(string field, string condition, string pattern, int? page)
        {
            ViewBag.Field = field;
            ViewBag.Pattern = pattern;
            ViewBag.Condition = condition;
            List<UserModelView> userModelViews = new List<UserModelView>();
            List<UserModel> userModels = new List<UserModel>();
            if (!String.IsNullOrEmpty(pattern))
            {
                Service1 service = new Service1();
                userModels = service.FilterUsers(field, condition, pattern).ToList();
            }
            foreach (UserModel userModel in userModels)
            {
                userModelViews.Add(new UserModelView(userModel));
            }
            int pageSize = 5;
            int pageNumber = (page ?? 1);
            return View(userModelViews.ToPagedList(pageNumber, pageSize));
        }
    }
}
